<?php
class Content_management extends MY_Controller{
	
 	function __construct()
    {
    	parent::__construct();
    }
	function _remap($method,$params = array())
	{
	   $this->_admin_remap($method,$params);	   
	}
	function index() {
		$this->load->model('Language');
		foreach($this->Language->get_all() as $row){
			$data['langList'][$row['language_code']] = $row['language_name'];
		}
    	$this->_setLayout('admin/content_management','admin_layout',$data);
    }
	function add_content(){
    	$callback = $_REQUEST['callback'];
    	$jsonOutPut = '';
		//try{
	    	$data = json_decode ($_GET['data'],true);
	    	if(empty($data)){
	    		$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not save'));	    			    		
	    	}
	    	else{
		    	$this->load->model('Content_html');
		    	if(isset($data['id'])){
		    		$this->Content_html->update($data,$data['id']);
		    		$data_return = $this->Content_html->get_by_id($data['id']);
		    		$data_return['edit'] = '<a href="'.base_url().'admin/content_management/edit_content/'.$data['id'].'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>';
		    		$jsonOutPut = json_encode(array('success'=>true,'message'=>'Saved successfully','data'=>$data_return));
		    	}	
		    	else{	
		    		$id = $this->Content_html->insert($data);
		    		$data_return = $this->Content_html->get_by_id($id);
		    		$data_return['edit'] = '<a href="'.base_url().'admin/content_management/edit_content/'.$id.'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>';
		    		$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','data'=>$data_return));
		    	}		 
	    	}
	    	   	
    	/*}
    	catch (Exception $e){
    		$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not save'));
    	}*/
    	if ($callback) {
		    header('Content-Type: text/javascript');
		    echo $callback . '(' . $jsonOutPut . ');';
		} else {
		    header('Content-Type: application/x-json');
		    echo $jsonOutPut;
		}	
    }
	function delete_content(){
		$callback = $_REQUEST['callback'];
		$jsonOutPut = '';
		try{
			$data = json_decode ($_GET['data'],true);			
    		$this->load->model('Content_html');
    		$this->Content_html->delete($data);
			$jsonOutPut =  json_encode(array('success'=>true));
		}
    	catch (Exception $e){
    		$jsonOutPut =  json_encode(array('success'=>false));
    	}
    	if ($callback) {
		    header('Content-Type: text/javascript');
		    echo $callback . '(' . $jsonOutPut . ');';
		} else {
		    header('Content-Type: application/x-json');
		    echo $jsonOutPut;
		}	
    }
    function content_grid(){
    	$this->load->model('Content_html');
	    $callback = $_REQUEST['callback'];
	    $data =  array();
	    $listContent = $this->Content_html->get_list($_GET['start'],$_GET['limit'],(isset($_GET['sort']))?$_GET['sort']:'id',(isset($_GET['dir']))?$_GET['dir']:'DESC');
	    foreach($listContent as $row){
	    	$data[] = array_merge($row,array('edit'=>'<a href="'.base_url().'admin/content_management/edit_content/'.$row['id'].'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>'));
	    }
		$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','start'=>$_GET['start'],'total'=>$this->Content_html->get_total(),'data'=>$data));
		if ($callback) {
		    header('Content-Type: text/javascript');
		    echo $callback . '(' . $jsonOutPut . ');';
		} else {
		    header('Content-Type: application/x-json');
		    echo $jsonOutPut;
		}
    }
	function edit_content($content_id=null){
		//die($content_id);		
		if(empty($content_id))
			header('Location: '.base_url().'admin/content_management/');
		else {		
			$this->load->model('Content_html');
			$this->load->model('Language');
			
			$message = null;
			if($this->input->server('REQUEST_METHOD') == 'POST'){
				if($this->Content_html->update($_POST,$content_id))
					$message = 'Update content successfully';
				else	
					$message = 'Update content fail';
			}
			$data = array('content' => $this->Content_html->get_by_id($content_id),
						  'message'=>$message);
			foreach($this->Language->get_all() as $row){
				$data['langList'][$row['language_code']] = $row['language_name']; 
			}			
			$this->_setLayout('admin/edit_content', 'admin_layout', $data);
		}	
	}
}